package demo19_Lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kunkun
 * @className Test03_ReentrantLock
 * @Description ReentrantLock外部锁获得自动锁内部
 * @date 2025/3/8 22:50
 */
public class Test03_ReentrantLock {
    public static void main(String[] args) {
        Test03_ReentrantLock test = new Test03_ReentrantLock();
        new Thread(()->{
            test.call();
        },"A").start();//A线程
        new Thread(()->{
            test.call();
        },"B").start();//B线程
    }
    private ReentrantLock lock =new ReentrantLock();

    public void call(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"打电话");
            sms();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }
    public  void sms() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"发邮件");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }


}
